package com.dunqi.gpm.shunqing.home.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.develop.activity.ProjectTrendDetailActivity;
import com.dunqi.gpm.shunqing.home.activity.CreateProjectTrendActivity;
import com.dunqi.gpm.shunqing.home.activity.ProjectTrendAutidActivity;
import com.dunqi.gpm.shunqing.home.adapter.ProjectWorkTrendAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.ProjectWorkTrend;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/12/4.
 */

public class ProjectWorkTrendFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private static final String DATA = "data";
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private HashMap<String, String> hashMap;
    ArrayList<ProjectWorkTrend> data = new ArrayList<>();
    private ProjectWorkTrendAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        hashMap.put("page", String.valueOf(page));
        hashMap.put("uda", UserUtil.userFromRespOrg().toString());
        hashMap.put("mainTrendKind", HomeFragment.homeMenuId== Constance.MENU_WORK_TREND?"2":"0");
        if (UserUtil.userIsLeader() && HomeFragment.homeMenuId== Constance.MENU_WORK_TREND)
            hashMap.put("audit_status","2");
        if (UserUtil.userFromNdrcOrg() && !UserUtil.userIsLeader() && HomeFragment.homeMenuId== Constance.MENU_DYNAMIC_APPROVAL)
            hashMap.put("audit_status","1");
        ApiFactory.getInstance()
                .queryOwnedProjectWorkTrendList(hashMap)
                .compose(RxSchedulers.<Page<ProjectWorkTrend>>io_main())
                .subscribe(new Consumer<Page<ProjectWorkTrend>>() {
                    @Override
                    public void accept(Page<ProjectWorkTrend> projectWorkTrendPage) throws Exception {
                        ArrayList<ProjectWorkTrend> list = projectWorkTrendPage.getList();
                        SwipeUtil.loadCompleted(swipeRefresh);

                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }

                        page++;

                        if (projectWorkTrendPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        adapter = new ProjectWorkTrendAdapter(data);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectWorkTrend trend = data.get(position);
        if ((trend.getAuditStatus()==Constance.AUDIT_STATUS_INIT || trend.getAuditStatus()==Constance.AUDIT_STATUS_REJECTED)
                && HomeFragment.homeMenuId==Constance.MENU_WORK_TREND && UserUtil.userFromNdrcOrg() && !UserUtil.userIsLeader()){
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("trendTitile", trend.getTrendTitile());
            hashMap.put("trendHtmlContent", trend.getTrendHtmlContent());
            hashMap.put("issueTime", trend.getIssueTime());
            CreateProjectTrendActivity.start((Activity) context, trend.getProWorkTrendId(),
                    trend.getTrendType(), hashMap);
        }
        else
            ProjectTrendDetailActivity.start((Activity)context,trend.getProWorkTrendId());


        if (true){
            return;
        }

        int levelType = UserUtil.getLevelType();
        int type = HomeFragment.homeMenuId;
        ProjectWorkTrend item = data.get(position);
        int auditStatus = item.getAuditStatus();



        if (UserUtil.userFromNdrcOrg() && type == 17 && auditStatus == 1) {
            //审核
            ProjectTrendAutidActivity.start(getActivity(),
                    item.getProWorkTrendId(),
                    400,
                    item.getProjectName());
        }

        if (UserUtil.userFromRespOrg() && type == 13 && (auditStatus == 3 || auditStatus == 0)) {
            //编辑
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("trendTitile", item.getTrendTitile());
            hashMap.put("trendHtmlContent", item.getTrendHtmlContent());
            hashMap.put("issueTime", item.getIssueTime());


            CreateProjectTrendActivity.start((Activity) context, item.getProWorkTrendId(),
                    item.getTrendType(), hashMap);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //CreateProjectTrendActivity
        if (resultCode == 600) {
            page = 1;
            net();
        }
        //ProjectTrendAutidActivity
        if (resultCode == 200) {
            page = 1;
            net();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_project_work_trend;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        hashMap = (HashMap<String, String>) arguments.getSerializable(DATA);

    }

    public static ProjectWorkTrendFragment newInstance(HashMap<String, String> hashMap) {
        ProjectWorkTrendFragment fragment = new ProjectWorkTrendFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DATA, hashMap);
        fragment.setArguments(bundle);
        return fragment;

    }

}

